***************************************
*EVENT STUDY WITH PAYDAY LOAN OPENINGS*
***************************************
use RECORD_NB anypaydayloan account_opened_m courtdate_m ///
 evicted monthsince numpaydayloan using "${build}/payday_panel_acct_linked.dta" if monthsince_p>=-48 & monthsince_p<=72, clear

egen unique_id2=group(RECORD_NB courtdate_m)

***********************************
*      Event Study Quarterly      *
***********************************
gen courtdate = dofm(courtdate_m)
format courtdate %td
gen case_quarter= qofd(courtdate)
format case_quarter %tq

gen account_open_date = dofm(account_opened_m)
format account_open_date %td
gen account_quarter= qofd(account_open_date)
format account_quarter %tq

gen payday_year=yofd(account_open_date)
gen qsince=account_quarter-case_quarter

bys unique_id qsince monthsince: gen first=_n==1

collapse (max) anypaydayloan (mean) evicted payday_year  (sum) first highest_credit numpaydayloan, by(qsince unique_id2)

*In order to be in the quarterly regression sample, the person needs 3 months per quarter
replace anypaydayloan=. if first~=3
replace numpaydayloan=. if first~=3

qui tab qsince, gen(qdum)
foreach num of numlist 1(1)41 {
		gen evictXq`num'=evicted*qdum`num'
}

local controls="i.payday_year"
fvset base 2011 payday_year

*Quarter variable
gen q_plot=.
foreach num of numlist 1(1)41 {
		local plotpos=`num'
		local rel_q=`num'-17
		replace q_plot=`rel_q'    in `plotpos'
}
replace q_plot=999 in 42
replace q_plot=9999 in 43

*Label variables
capture label var anypaydayloan "Any Payday Account * 100"
capture label var numpaydayloan "Number of Payday Loans"

local vnum=1
foreach var of varlist anypaydayloan numpaydayloan {
	gen var`vnum'_q_est=.
	gen var`vnum'_qXevict_est=.
	gen var`vnum'_q_se=.
	gen var`vnum'_qXevict_se=.
	gen str var`vnum'_name=`"`:variable label `var''"' in 1/43
	
	noi disp "`var'"
	
	reg `var' evicted qdum1-qdum4 qdum6-qdum41 evictXq1-evictXq4 evictXq6-evictXq41 `controls'
	
	sum `var' if evicted==0 & qdum5==1
	replace var`vnum'_q_est=r(mean) if q_plot==999
	replace var`vnum'_q_est=_b[evicted] if q_plot==9999
	
	foreach num of numlist 1(1)4 6(1)41 {
		local plotpos=`num'
		local rel_q=`num'-17
		replace var`vnum'_q_est=_b[qdum`num'] in `plotpos'
		replace var`vnum'_qXevict_est=_b[evictXq`num'] in `plotpos'
		replace var`vnum'_q_se=_se[qdum`num'] in `plotpos'
		replace var`vnum'_qXevict_se=_se[evictXq`num'] in `plotpos'
	}
	local vnum=`vnum'+1
}
keep q_plot var*
keep if _n<=43
tostring q_plot, replace
replace q_plot="Non-evict mean t=-12" if q_plot=="999"
replace q_plot="Evict coefficient" if q_plot=="9999"
export delimited using "${Inputs}/Cook_payday_accounts_eventstudy.csv", replace

****************************************
*EVENT STUDY WITH PAYDAY LOAN INQUIRIES*
****************************************
use RECORD_NB anyinq num_inq payday_inq_m courtdate_m ///
 evicted monthsince using "${build}/payday_panel_inq_linked.dta", clear


egen unique_id2=group(RECORD_NB courtdate_m) 

gen courtdate = dofm(courtdate_m)
format courtdate %td
gen case_quarter= qofd(courtdate)
format case_quarter %tq

gen account_open_date = dofm(payday_inq_m)
format account_open_date %td
gen account_quarter= qofd(account_open_date)
format account_quarter %tq

gen payday_year=yofd(account_open_date)
gen qsince=account_quarter-case_quarter

bys unique_id qsince monthsince: gen first=_n==1

collapse (max) anyinq (mean) evicted payday_year (sum) first num_inq, by(qsince unique_id2)

*In order to enter the quarterly regression, the person needs 3 months per quarter
replace anyinq=. if first~=3
replace num_inq=. if first~=3

*Label variables
label var  anyinq "Any Payday Inquiry * 100"
label var  num_inq "Number of Payday Inquiries"

qui tab qsince, gen(qdum)
foreach num of numlist 1(1)41 {
		gen evictXq`num'=evicted*qdum`num'
}

local controls="i.payday_year"
fvset base 2011 payday_year

*Quarter variable
gen q_plot=.
foreach num of numlist 1(1)41 {
		local plotpos=`num'
		local rel_q=`num'-17
		replace q_plot=`rel_q'    in `plotpos'
}
replace q_plot=999 in 42
replace q_plot=9999 in 43

local vnum=1
foreach var of varlist anyinq num_inq {
	gen var`vnum'_q_est=.
	gen var`vnum'_qXevict_est=.
	gen var`vnum'_q_se=.
	gen var`vnum'_qXevict_se=.
	gen str var`vnum'_name=`"`:variable label `var''"' in 1/43
	
	noi disp "`var'"
	
	reg `var' evicted qdum1-qdum4 qdum6-qdum41 evictXq1-evictXq4 evictXq6-evictXq41 `controls'
	sum `var' if evicted==0 & qdum5==1
	replace var`vnum'_q_est=r(mean) if q_plot==999
	replace var`vnum'_q_est=_b[evicted] if q_plot==9999
	
	foreach num of numlist 1(1)4 6(1)41 {
		local plotpos=`num'
		local rel_q=`num'-17
		replace var`vnum'_q_est=_b[qdum`num'] in `plotpos'
		replace var`vnum'_qXevict_est=_b[evictXq`num'] in `plotpos'
		replace var`vnum'_q_se=_se[qdum`num'] in `plotpos'
		replace var`vnum'_qXevict_se=_se[evictXq`num'] in `plotpos'
	}
	local vnum=`vnum'+1
}
keep q_plot var*
keep if _n<=43
tostring q_plot, replace

replace q_plot="Non-evict mean t=-12" if q_plot=="999"
replace q_plot="Evict coefficient" if q_plot=="9999"
export delimited using "${Inputs}/Cook_payday_inquiries_eventstudy.csv", replace
